package cn.jobstart.spider.pageProcessor;

import cn.jobstart.common.StringUtil;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.spider.model.NbrcCompany;
import cn.jobstart.spider.model.NbrcJob;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @version 1.0
 * @Description 宁波人才网
 * @date 2023-09-18 15:35
 */
@Data
public class NbrcJobPageProcessor extends AbstractPageProcessor {


    private static String JOB_LIST = "https://www.nbrc.com.cn/baseApp/app/search/job";
    private static String JOB_DETAIL = "https://www.nbrc.com.cn/baseApp/app/job/getJobDetail";
    private static String COMPANY_DETAIL = "https://www.nbrc.com.cn/baseApp/app/company/companyInfo";


    @Override
    public int getChannelFlag() {
        return TalentSubscriptionTaskChannelEnum.NBRC.getType().intValue();
    }


    private void processJobList(Page page) {

        JSONObject jsonObject = JSONObject.parseObject(page.getRawText());

        if (StringUtil.isNullOrEmpty(jsonObject) || StringUtil.isNullOrEmpty(jsonObject.get("data"))) {
            return;
        }

        JSONObject dataJson = jsonObject.getJSONObject("data");
        JSONObject pageJson = dataJson.getJSONObject("page");
        String dataArray = dataJson.getString("list");

        int currentPage = pageJson.getIntValue("currentPage");
        int totalPage = pageJson.getIntValue("totalPage");


        List<NbrcJob> jobList = JSONArray.parseArray(dataArray, NbrcJob.class);

        if (StringUtil.isNullOrEmpty(jobList)) {
            return;
        }

        List<String> companyIds = jobList.stream().map(NbrcJob::getCompanyId).distinct().collect(Collectors.toList());
        List<String> jobIds = jobList.stream().map(NbrcJob::getId).distinct().collect(Collectors.toList());


        if (StringUtil.isNotObjEmpty(companyIds)) {

            companyIds.stream().forEach(companyId -> {

                Request request = new Request(COMPANY_DETAIL + "/" + companyId);
                request.setMethod(HttpConstant.Method.GET);

                request.setRequestBody(HttpRequestBody.json("{}", "utf-8"));
                page.addTargetRequest(request);

               // page.addTargetRequest(COMPANY_DETAIL + "/" + companyId);
            });


        }
        if (StringUtil.isNotObjEmpty(jobIds)) {

            jobIds.stream().forEach(jobId -> {

                Request request = new Request(JOB_DETAIL + "?jobId=" + jobId);
                request.setMethod(HttpConstant.Method.POST);

                request.setRequestBody(HttpRequestBody.json("{}", "utf-8"));
                page.addTargetRequest(request);

            });


        }


        page.setSkip(true);

        if (currentPage == totalPage) {
            return;
        }


        Request request = new Request("https://www.nbrc.com.cn/baseApp/app/search/job?pageSize=100&pageNumber=" + (currentPage + 1));
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json("{}", "utf-8"));
        page.addTargetRequest(request);

    }


    public void processJobDetail(Page page) {



        JSONObject jsonObject = JSONObject.parseObject(page.getRawText());


        page.getResultItems().put("jobDetail",jsonObject.getJSONObject("data").getJSONObject("fullJobVo").getJSONObject("jobVo").toJavaObject(NbrcJob.class));





    }

    public void processCompanyDetail(Page page) {

        JSONObject jsonObject = JSONObject.parseObject(page.getRawText());

        page.getResultItems().put("companyDetail",jsonObject.getJSONObject("data").getJSONObject("companyInfo").getJSONObject("company").toJavaObject(NbrcCompany.class));


      //  System.out.println(page);

    }

    /**
     * process the page, extract urls to fetch, extract the data and store
     *
     * @param page page
     */
    @Override
    public void process(Page page) {

        String url = page.getRequest().getUrl();
        //说明是列表
        if (url.indexOf(JOB_LIST) >= 0) {
            processJobList(page);
        } else if (url.indexOf(JOB_DETAIL) >= 0) {
            //说明是职位详情
            processJobDetail(page);
        } else if (url.indexOf(COMPANY_DETAIL) >= 0) {
            //说明公司详情
            processCompanyDetail(page);
        }


    }

    /**
     * get the site settings
     *
     * @return site
     * @see Site
     */
    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3).setSleepTime(1000)
                .addHeader("Keyid", "bb5259528637fe5466a8d77128dd01c2")
                .addHeader("nbrcbefore", "1695024686000")
                .addHeader("nbrcafter", "c5643ef2ce7a613a2437b7a15b4e5977")
                .addHeader("jsontype", "jsonType")


                ;
       /* return Site.me().setRetryTimes(3).setSleepTime(1000)

                .addHeader("adminauthorization","admintoken eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJhZG1pbklkXCI6XCIxNWRiODVhOWE3ZGE0MjAzYmQ3MDIzMjVhZjVhMWU1YlwiLFwiY3JlYXRlVGltZVwiOjE2OTUwNDM4OTU5MjYsXCJyZWRpc0tleVwiOlwiYzUxMTZjYjM4ZGQ4NDRjMmI5MzAzZjRjZGVkMmUwZjdcIixcInRydWVuYW1lXCI6XCJhZG1pblwiLFwidXNlcm5hbWVcIjpcImFkbWluXCJ9IiwiZXhwIjoxNjk1MTMwMjk1fQ.VmkwYW1isEf13FJcX0nXeLc5bCNi2DAcaY_mbSUO_qSHgEpSyf3GxRfmehWv4YUfdkqIYvM9RSzjfeF9gGjCMQ")




                ;*/
    }
}
